package com.gmail.zstate.rtspcam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import android.hardware.Camera;
import android.view.SurfaceView;

public class TCPServer implements Runnable {
	
	public static final int PORT = 6789;
	
	private ServerSocket servSocket = null;
	
	private Socket socket = null;

	private SurfaceView videoView;
	
	private Camera camera;
	
	public Camera getCamera() {
		return camera;
	}

	public void setCamera(Camera camera) {
		this.camera = camera;
	}

	public SurfaceView getVideoView() {
		return videoView;
	}

	public void setVideoView(SurfaceView videoView) {
		this.videoView = videoView;
	}

	public TCPServer(ServerSocket servSocket) {
		setServSocket(servSocket);
	}
	
	/**
	 * @param servSocket
	 */
	public void run() {
		while(true) {
		    //Accept the incoming socket, which means that the server process will
		    //wait until the client connects, then prepare to handle client commands
			try {
				this.socket = servSocket.accept();
				socket.setReceiveBufferSize(500000);
				socket.setSendBufferSize(500000);
				System.out.println("Client socket created and connected to server socket...");
				handleClient(socket); //Call handleClient method
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
	}

	private void handleClient(Socket newDataSocket) {
		String content;
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			String requestHeader = input.readLine();
			content = requestParser(requestHeader);
			//generateResponse(newDataSocket, content);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param newDataSocket
	 * @param content
	 * @throws IOException
	 */
	private void generateResponse(Socket newDataSocket, String content)
			throws IOException {
		PrintWriter writer;
		writer = new PrintWriter(newDataSocket.getOutputStream());
		writer.write("HTTP/1.1 200 OK\r\n");// Return status code for OK (200)
		writer.write("Content-Length: " + (int) content.length() + "\r\n");
		writer.write("Content-Type: text/html; charset=utf-8\r\n\r\n");
		writer.write(content);
		writer.flush();
		this.socket.close();
	}
	
	public String requestParser(String request) {
		String context = "";
		String[] requestArray = request.split(" ");
		String[] path = requestArray[1].split("/");
		if (path[1].equals("show")) {
			context = "this is show action";
		} else if(path[1].equals("broadcast")) {
			//context = "this is broadcast action";
		} else if(path[1].equals("stop")) {
			context = "stop action";
			//currentThread.interrupt();
		}
		return context;
	}
	
	public ServerSocket getServSocket() {
		return servSocket;
	}

	public void setServSocket(ServerSocket servSocket) {
		this.servSocket = servSocket;
	}
	
	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}
}